package com.hpdz.hotel_back.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpdz.hotel_back.entity.Admin;
import com.hpdz.hotel_back.entity.Employee;
import com.hpdz.hotel_back.mapper.AdminMapper;
import com.hpdz.hotel_back.mapper.EmployeeMapper;
import com.hpdz.hotel_back.service.AdminService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;

@Transactional
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private AdminMapper adminMapper;

    @Override
    public Admin login(Admin admin) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();

        adminQueryWrapper.eq(true, "email", admin.getEmail());
        adminQueryWrapper.eq(true, "passwd", admin.getPassword());

       return getOne(adminQueryWrapper);
    }


    @Override
    public IPage<Employee> getAllEmp(Integer current, Integer size) {
        Page<Employee> employeePage = new Page<>(current, size);
        return employeeMapper.selectPage(employeePage,null);
    }

    @Override
    public boolean modifyEmployee(Employee emp) {
        if(emp.getEmpId() == null) return false; //todo: 这里最好抛异常,先这样后面再说!
        return employeeMapper.updateEmp(emp) == 1;
    }

    @Override
    public boolean addEmp(Employee emp) {
        if(emp == null) return false; //todo: 这里最好抛异常,先这样后面再说!
        return employeeMapper.insert(emp) == 1;
    }

    @Override
    public boolean modifyAdmin(Admin admin) {
        if(admin == null) return false; //todo: 这里最好抛异常,先这样后面再说!
        return adminMapper.updateAdmin(admin) == 1;
    }

    @Override
    public IPage<Employee> searchEmpPage(Integer current, Integer size, Employee emp) {
        System.out.println(emp);
        Page<Employee> page = new Page<>(current,size);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(emp.getNickName()!= null,Employee::getNickName,emp.getNickName());
        return employeeMapper.selectPage(page,wrapper);
    }

    @Override
    public boolean deleteEmp(Integer id) {
        if(id == null) return false; //todo: 这里最好抛异常,先这样后面再说!
        return employeeMapper.deleteById(id) == 1;
    }

    @Override
    public Boolean register(Admin admin) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("email", admin.getEmail());
        Admin admin1 = adminMapper.selectOne(wrapper);
        if(admin1 != null) return false;
        return save(admin);
    }

}
